
/**   
*    
* 项目名称：a_yongan   
* 类名称：OutsourcingInServiceImpl   
* 类描述：   
* 创建人：liubo   
* 创建时间：2018年6月30日 上午10:59:50    
* 修改时间：2018年6月30日 上午10:59:50   
* 修改备注：   
* @version    
*    
*/
package com.yongan.warehouse.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import com.yongan.user.enums.UserStatEnum;
import com.yongan.user.exception.UserException;
import com.yongan.util.Execution;
import com.yongan.warehouse.dao.TransferDao;
import com.yongan.warehouse.dao.TransferDetailsDao;
import com.yongan.warehouse.entity.Transfer;
import com.yongan.warehouse.entity.TransferDetails;
import com.yongan.warehouse.service.TransferService;

/**
 * @author liubo
 *
 */
@Service
public class TransferServiceImpl implements TransferService{

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private TransferDao transferDao;
	@Autowired
	private TransferDetailsDao transferDetailsDao;
	
	//private GoodsinformationDao goodsinformationDao;
	private final String slat = "dsjdajsdkHJJHAJAKdsd921e0p12*DAe#@$@!qew";
	@Override
	public List<Transfer> getTransferList(int offset, int limit) {
		return transferDao.queryAll(offset, limit);
	}

	
	@Override
	public Transfer getTransferById(String transferId) {
		// TODO 自动生成的方法存根
		Transfer transfer = transferDao.queryById(transferId);
		transfer.setDetails(transferDetailsDao.queryAllByTopId(transferId));
		return transfer;
	}

	

	@Override
	@Transactional
	public Execution executeTransfer(Transfer transfer,
			String transferId, String md5) throws UserException {
		if(md5 == null || !md5.equals(getMD5(transferId))){
			//throw new UserException("user data rewrite");
		}
		//System.out.println("otherGoodOutId:"+transferId);
		//System.out.println(transfer.toString());
		List<TransferDetails> details =null;
		try{
			if(transfer == null){
				Transfer readDell = transferDao.queryById(transferId);
				details = readDell.getDetails();
				//调拨暂不更改库存
				/*if(!(details==null)){
					for(OutGoodDetails goodDetails:details){
						reduceNumber(goodDetails, "add");
					}
				}*/
				int deleteCount = transferDao.deleteTransfer(transferId);
				int deleteInGoodDetailsCount = transferDetailsDao.deleteAllTransferDetails(transferId);
				if(deleteCount<=0 & deleteInGoodDetailsCount<=0){
					throw new UserException("delete failed");
				}else{
					return new Execution(transferId, UserStatEnum.SUCCESS);
				}
			}else if((transferId.equals(""))){
				//Integer hjrksl= 0 ;
				//Integer hjsl= 0 ;
				Integer zje= 0 ;
				
				details = transfer.getDetails();
				int goodDetailsaddcount = 1;
				if(!(details==null)){
					for(TransferDetails goodDetails:details){
						//hjrksl = hjrksl + Integer.parseInt(goodDetails.getOutgooddetailsCksl());
						//hjsl = hjsl + Integer.parseInt(goodDetails.getOutgooddetailsSl());
						zje = zje + Integer.parseInt(goodDetails.getTransferdetailsDcje());
						goodDetails.setTransferdetailsId(UUID.randomUUID().toString());
						goodDetails.setTopId(transfer.getTransferId());
						//goodDetails.setTransferdetailsType("qtckd");
						int addInGoodDetailsCount = transferDetailsDao.insertTransferDetails(goodDetails);
						if(addInGoodDetailsCount<=0){
							goodDetailsaddcount = addInGoodDetailsCount;
						}
						//reduceNumber(goodDetails, "reduce");//调拨暂不更改库存
					}
				}
				//transfer.setOthergoodoutHjrksl(String.valueOf(hjrksl));
				//transfer.setOthergoodoutHjsl(String.valueOf(hjsl));
				transfer.setTransferHjdcje(String.valueOf(zje));
				int addCount = transferDao.insertTransfer(transfer);
				if(addCount<=0 & goodDetailsaddcount<=0){
					throw new UserException("add failed");
				}else{
					return new Execution(transfer.getTransferId(), UserStatEnum.SUCCESS);
				}
			}else if(!(transferId.equals(""))){
				List<TransferDetails> oldList = transferDetailsDao.queryAllByTopId(transferId);
				List<String> newList = new ArrayList<String>();
				//String[] newList;
				details = transfer.getDetails();
				int goodDetailscount = 1;
				//Integer hjrksl= 0 ;
				//Integer hjsl= 0 ;
				Integer zje= 0 ;
				if(!(details==null)){
					for(TransferDetails goodDetails:details){
						//减去新保存的列表
						//reduceNumber(goodDetails, "reduce");
						//hjrksl = hjrksl + Integer.parseInt(goodDetails.getOutgooddetailsCksl());
						//hjsl = hjsl + Integer.parseInt(goodDetails.getOutgooddetailsSl());
						zje = zje + Integer.parseInt(goodDetails.getTransferdetailsDcje());
						//如果更新的总单据下面的子单据ID为空，则为新增，否则是修改
						if(goodDetails.getTransferdetailsId().equals("")){
							goodDetails.setTransferdetailsId(UUID.randomUUID().toString());
							goodDetails.setTopId(transfer.getTransferId());
							//goodDetails.setOutgooddetailsType("qtckd");
							int ua = transferDetailsDao.insertTransferDetails(goodDetails);
							if(ua<=0){
								goodDetailscount = ua;
							}
						}else{
							//reduceNumber(goodDetails, "reduce");
							int uu = transferDetailsDao.updateTransferDetails(goodDetails);
							if(uu<=0){
								goodDetailscount = uu;
							}else{
								newList.add(goodDetails.getTransferdetailsId());
							}
						}
					}
				}
				if (oldList != null) {
					for (TransferDetails old : oldList) {
						// 减去旧列表在库存中的数量
						//reduceNumber(old, "add");
						Boolean sf = false;
						if (newList != null) {
							for (String newl : newList) {
								if (old.getTransferdetailsId().equals(newl)) {
									sf = true;
								}
							}
							if (!sf) {
								transferDetailsDao.deleteTransferDetails(old.getTransferdetailsId());
							}
						}
					}
				}

				//transfer.setOthergoodoutHjrksl(String.valueOf(hjrksl));
				//transfer.setOthergoodoutHjsl(String.valueOf(hjsl));
				transfer.setTransferHjdcje(String.valueOf(zje));
				int updateCount = transferDao.updateTransfer(transfer);
				if(updateCount<=0 & goodDetailscount<=0){
					throw new UserException("update failed");
				}else{
					return new Execution(transferId, UserStatEnum.SUCCESS);
				}
			}else{
				throw new UserException("parameter error");
			}
			
		}catch(Exception e){
			logger.error(e.getMessage(),e);
			throw new UserException("inner error:"+e.getMessage());
		}
	}

	
	@Override
	public int getCount() {
		// TODO 自动生成的方法存根
		return transferDao.queryCount();
	}
	
	private String getMD5(String Id){
		String base = Id + "/" + slat;
		String md5 = DigestUtils.md5DigestAsHex(base.getBytes());
		return md5;
	}
	
	/*public TransferServiceImpl() {
		super();
		// TODO 自动生成的构造函数存根
	}*/


	/*public int reduceNumber(OutGoodDetails igd,  String type){
		return goodsinformationDao.reduceNumber(igd.getOutgooddetailsNumber()
				, type, Integer.parseInt(igd.getOutgooddetailsSl())
				, Integer.parseInt(igd.getOutgooddetailsCksl()));
	}*/
	
}
